/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#ifndef __ISA_RISCV64_H__
#define __ISA_RISCV64_H__

#include <common.h>
#include <stdbool.h>
#include <stdint.h>

typedef struct {
    union {
        struct {
            uint32_t WPRI_0 : 1;
            uint32_t SIE    : 1;
            uint32_t WPRI_1 : 1;
            uint32_t MIE    : 1;
            uint32_t WPRI_2 : 1;
            uint32_t SPIE   : 1;
            uint32_t UBE    : 1;
            uint32_t MPIE   : 1;
            uint32_t SPP    : 1;
            uint32_t VS     : 2;
            uint32_t MPP    : 2;
            uint32_t FS	    : 2;
            uint32_t XS     : 2;
            uint32_t MPRV   : 1;
            uint32_t SUM    : 1;
            uint32_t MXR    : 1;
            uint32_t TVM    : 1;
            uint32_t TW     : 1;
            uint32_t TSR    : 1;
            uint16_t WPRI_3 : 9;
            uint32_t UXL    : 2;
            uint32_t SXL    : 2;
            uint32_t SBE    : 1;
            uint32_t MBE    : 1;
            uint64_t WPRI_4 : 25;
            uint32_t SD     : 1;
        };
        word_t value;
    } mstatus;
    union {
        struct {
            uint8_t USIE   : 1;
            uint8_t SSIE   : 1;
            uint8_t WPRI_0 : 1;
            uint8_t MSIE   : 1;
            uint8_t UTIE   : 1;
            uint8_t STIE   : 1;
            uint8_t WPRI_1 : 1;
            uint8_t MTIE   : 1;
            uint8_t UEIE   : 1;
            uint8_t SEIE   : 1;
            uint8_t WPRI_2 : 1;
            uint8_t MEIE   : 1;
            uint64_t WPRI_3 : 52;
        };
        word_t value;
    } mie;
    union {
        struct {
            uint64_t mode   : 2;
            uint64_t base   : 62;
        };
        word_t value;
    } mtvec;
    union {
        word_t value;
    } mepc;
    union {
        struct {
            uint64_t code   : 63;
            uint64_t intr   : 1;
        };
        word_t value;
    } mcause;
    union {
        struct {
            uint8_t USIP   : 1;
            uint8_t SSIP   : 1;
            uint8_t WPRI_0 : 1;
            uint8_t MSIP   : 1;
            uint8_t UTIP   : 1;
            uint8_t STIP   : 1;
            uint8_t WPRI_1 : 1;
            uint8_t MTIP   : 1;
            uint8_t UEIP   : 1;
            uint8_t SEIP   : 1;
            uint8_t WPRI_2 : 1;
            uint8_t MEIP   : 1;
            uint64_t WPRI_3 : 52;
        };
        word_t value;
    } mip;
} riscv64_CSRs;

typedef struct {
    word_t gpr[32];
    vaddr_t pc;
    // CSRs
    riscv64_CSRs csr;
} riscv64_CPU_state;

// decode
typedef struct {
    union {
        uint32_t val;
    } inst;
} riscv64_ISADecodeInfo;

#define isa_mmu_check(vaddr, len, type) (MMU_DIRECT)

#endif
